package com.lg.erp.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SCompany;
import com.lg.erp.service.ISCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lg
 * @since 2021-04-20
 */
@RestController
public class SCompanyController {

    @Autowired
    private ISCompanyService companyService;
    //查询所有公司
    @RequestMapping("/company")
    public JsonResult getAllCompany(){
        List<SCompany> list = companyService.list();
        return new JsonResult(1,"ok",list);
    }
    @RequestMapping("/company/{current}")
    public JsonResult getCompanyByPage(@PathVariable Integer current){
        Page<SCompany> page=new Page<>(current,10);
        IPage<SCompany> pages=companyService.getCompanyByPage(page);
        return new JsonResult(1,"ok",pages);
    }
    //模糊查询公司
    @RequestMapping("/company/{keyWord}/{province}/{city}/{area}/{current}")
    public JsonResult findCompanyByKeyWordAndPage(@PathVariable String keyWord
            ,@PathVariable String province
            ,@PathVariable String city
            ,@PathVariable String area
            ,@PathVariable Integer current){
            Page<SCompany> page=new Page<>(current,10);
            IPage<SCompany> pages=companyService.getCompanyByCompanyNameAndPage((keyWord.equals(" ")?"":keyWord)
                    ,(province.equals(" ")?"":province),(city.equals(" ")?"":city),(area.equals(" ")?"":area),page);
        return new JsonResult(1,"ok",pages);
    }

    @RequestMapping(value = "/company",method = RequestMethod.POST)
    public JsonResult saveCompany(@RequestBody SCompany company){
        company.setId(UUID.randomUUID().toString().replace("-",""));
        companyService.save(company);
        return new JsonResult(1,"ok",company);
    }

    @RequestMapping(value = "/company",method = RequestMethod.PUT)
    public JsonResult updateCompanyById(@RequestBody SCompany company){
        companyService.updateById(company);
        return new JsonResult(1,"ok",company);
    }
}
